package com.twu.biblioteca;

import com.twu.model.Book;
import com.twu.model.Movie;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * Created by Janu on 21/06/14.
 */
public class OptionManager {

    TreeSet<Book> books = new TreeSet<Book>(new myBooksComp());
    TreeSet<Movie> movies = new TreeSet<Movie>(new myMoviesComp());
    TreeSet<Customer> customers = new TreeSet<Customer>(new myCustomerComp());

    OptionManager(){

        DataSet dataSet = new DataSet();

        this.books = dataSet.getBookDataSet();
        this.movies = dataSet.getMoviesDataSet();
        this.customers = dataSet.getCustomersDataSet();

    }

    public TreeSet getBooks(){
        return this.books;
    }

    public String getListOfBooks(){

        String listOfBooks = "";

        Iterator iter = this.books.iterator();

        Book aBooksList;
        listOfBooks += "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
        listOfBooks += String.format("%5s%75s%30s%15s","Code","Book Name","Author","Year Published");
        listOfBooks += "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

        while(iter.hasNext()) {
            aBooksList = (Book) iter.next();

            if (aBooksList.getBookAvailability()) {

                listOfBooks += String.format("%5s%75s%30s%5d\n", aBooksList.getBookCode(), aBooksList.getBookName(), aBooksList.getBookAuthor(), aBooksList.getBookYearPublished());

            }
        }

        return listOfBooks;

    }

    public String getReturnBook(Customer customer, String input){

        String output = "";
        Iterator iter = this.books.iterator();

        Book newBook;
        boolean flag = false;

        try{

            while(iter.hasNext()) {
                newBook = (Book) iter.next();
                if(newBook.getBookCode().equals(input)){
                    if (newBook.getBookAvailability()){
                        flag = true;
                    }
                    else{
                        if (newBook.getBookCheckedOutBy().equals(customer.getCustNumber())) {
                            newBook.setBookAvailable();
                            newBook.setBookCheckedOutBy("");
                            output = "\nThank you for returning the book.";
                            flag = false;
                            break;
                        } else {
                            output = "\nNot a valid customer to return";
                            flag = false;
                            break;
                        }

                    }
                }else{
                    flag=true;
                }

            }
            if(flag){
                output = "\nThat is not a valid book to return.";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return output;
    }

    public String getCheckOutBook(Customer customer, String input){

        String output="";
        int count=0;
        Iterator iter = this.books.iterator();
        Book newBook;
        boolean flag = false;

        try{
            while(iter.hasNext()){
                newBook = (Book) iter.next();
                if(newBook.getBookCode().equals(input)){
                    if(newBook.getBookAvailability()){
                        newBook.setBookNotAvailable();
                        newBook.setBookCheckedOutBy(customer.getCustNumber());
                        output = "\nThank you! Enjoy the book";
                        flag = false;
                        break;
                    }
                    else{
                        output = "\nThe book is not available";
                        flag = false;
                        count++;
                    }
                }
                else{
                    flag = true;
                }
            }
            if(flag && count==books.size()){
                output = "\nNot a valid code!";
            }

        }catch(Exception e){
            e.printStackTrace();
        }
        return output;

    }

    public String getListOfMovies(){

        Iterator iter = this.movies.iterator();

        String listOfMovies = "";

        Movie aMoviesList;
        listOfMovies += "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
        listOfMovies += String.format("%5s%50s%5s%25s%7s","Code","Movie Name","Year","Director","Rating");
        listOfMovies += "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

        while(iter.hasNext()) {
            aMoviesList = (Movie) iter.next();

            if (aMoviesList.getMovAvailability()) {

                listOfMovies += String.format("%5s%50s%5d%25s%7d\n", aMoviesList.getMovCode(),aMoviesList.getMovName(),aMoviesList.getMovYear(), aMoviesList.getMovDirector(), aMoviesList.getMovRating());

            }
        }

        return listOfMovies;
    }

    public String getReturnMovie(Customer customer, String input){

        String output="";
        Iterator iter = this.movies.iterator();
        Movie newMovie;
        boolean flag = false;

        try{

            while(iter.hasNext()) {
                newMovie = (Movie) iter.next();
                if (newMovie.getMovCode().equals(input)) {
                    if (newMovie.getMovAvailability()) {
                        flag = true;
                    } else {
                        if (newMovie.getMovCheckedOutBy().equals(customer.getCustNumber())) {
                            newMovie.setMovAvailable();
                            newMovie.setMovCheckedOutBy("");
                            output = "\nThank you for returning the movie.";
                            flag = false;
                            break;
                        } else {
                            output = "\nNot a valid customer to return";
                            flag = false;
                            break;
                        }

                    }

                }
                else{
                    flag=true;
                }

            }
            if (flag) {
                output = "\nThat is not a valid movie to return.";
            }


        }catch (Exception e){
            e.printStackTrace();
        }
        return output;
    }

    public String getCheckOutMovie(Customer customer, String input){

        String output="";
        Iterator iter = this.movies.iterator();
        Movie newMovie;
        boolean flag = false;
        int count=0;

        try{
            while(iter.hasNext()){
                newMovie = (Movie) iter.next();
                if(newMovie.getMovCode().equals(input)){
                    if(newMovie.getMovAvailability()){
                        newMovie.setMovNotAvailable();
                        newMovie.setMovCheckedOutBy(customer.getCustNumber());
                        output = "\nThank you! Enjoy the movie";
                        flag = false;
                        break;
                    }
                    else{
                        output = "\nThe movie is not available";
                        flag = false;
                        count++;
                    }
                }
                else{
                    flag = true;
                }
            }
            if(flag && count==movies.size()){
                output = "\nNot a valid code!";
            }


        }catch(Exception e){
            e.printStackTrace();
        }
        return output;
    }

    public String getCustomerInformation(Customer customer){

        return "Number: " + customer.getCustNumber() + "\nName: " + customer.getCustName() + "\nEmail: " + customer.getCustEmail() + "\nPhone: " + customer.getCustPhone();

    }

    public String getBookOrMovieCodeFromUser(){

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter code: ");

        try {
            return br.readLine();
        }catch(Exception e){
            e.printStackTrace();
        }
        return "";
    }

}
